﻿using com.iot.core.boot;
using com.iot.core.data;
using com.iot.core.data.entity;
using com.iot.core.data.service;
using com.iot.core.exception;
using com.iot.core.infrastructure;
using com.iot.core.manager.id;
using com.iot.ht.Controllers.htmenu.data;
using com.iot.ht.Controllers.htpage.data;
using com.iot.ht.Controllers.htrole.data;
using com.iot.ht.Controllers.htuser.data;
using com.iot.ht.Controllers.htuserlogin.data;
using Icom.iot.core.ext;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using System.Threading;

namespace com.iot.ht.Controllers.htuserlogin.service
{
    public class HtUserLoginService : BaseService<HtUserLogin>, IHtUserLoginService
    {       
 

        public async Task<Page<HtUserLogin>> PageAsync(HtUserLoginQto mm)
        {
            var q = BaseMapper.GetAll();
            if (!string.IsNullOrWhiteSpace(mm.Dtb))
            {
                q = q.Where(a => a.CrtDt >= mm.GetDB());
            }
            if (!string.IsNullOrWhiteSpace(mm.Dte))
            {
                q = q.Where(a => a.CrtDt < mm.GetDE());
            }
            if (!string.IsNullOrEmpty(mm.Key))
            {
                q = q.Where(a => a.HtdeptName.Contains(mm.Key)
                    || a.Name.Contains(mm.Key)
                     
                );
            }
            if (!string.IsNullOrEmpty(mm.State))
            {
                var bIsOnline = mm.State.Equals("在线");
                q = q.Where(a =>a.Online==bIsOnline

                );
            }
            var r = await BaseMapper.PageAsync(q, mm);
            return r;
        }
        public int ForceLx(BaseQto mm)
        {
            var m = BaseMapper.GetById(mm);
            if (m.Online )
            {
                m.Online=false;

                var myContext = EngineContext.Current.Resolve<MyContext>();
                myContext.SetLogMemo($"强制{m.Name} 离线");

                return Update(m);
            }
            return 0;
        }
        public int SetDebug(HtUserLoginDebugDto mm)
        {
            var m = BaseMapper.GetById(mm.Id);
            if (m.Debug!=mm.Debug)
            {
                m.Debug = !m.Debug;

                var myContext = EngineContext.Current.Resolve<MyContext>();
                myContext.SetLogMemo($"设置{m.Name} 调试模式{mm.Debug}");

                return Update(m);
            }
            return 0;
        }
    }
}
